package com.hutiao.snowhb.actions.mm;

import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.hutiao.snowhb.common.DateTimeUtil;
import com.hutiao.snowhb.common.WeixinUtil;
import com.hutiao.snowhb.entities.SnowMM;
import com.hutiao.snowhb.entities.SnowMMPrebook;
import com.hutiao.snowhb.entities.WeixinUserInfo;
import com.hutiao.snowhb.resource.ConfigInfo;
import com.hutiao.snowhb.service.SnowMMService;
import com.hutiao.snowhb.service.SnowPrebookService;
import com.hutiao.snowhb.wechat.vo.WxCustomText;
import com.hutiao.snowhb.wechat.vo.WxCustomTextMsg;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/snowmm")
public class SnowPrebookAction extends ActionSupport {
	
	private static Logger logger = Logger.getLogger(SnowPrebookAction.class);

	private static final long serialVersionUID = 7310525906821939440L;
	
	@Autowired
	private SnowMMService mmService;
	
	@Autowired
	private SnowPrebookService prebookService;
	
	private SnowMMPrebook book;
	
	private String result;
	
	@JSON(serialize=false)
	public SnowMMPrebook getBook() {
		return book;
	}

	public void setBook(SnowMMPrebook book) {
		this.book = book;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	@Action(value="prebook",results = { @Result(type = "json") })
	public String preBook() {
		
		try {
			this.prebookService.savePrebook(this.book);
			this.result = "success";
		} catch (SQLException e) {
			this.result = "error";
			logger.error("SQLException", e);
			return SUCCESS;
		}
		
		// 向mm发送通知
		
		
		try {
			SnowMM mm = this.mmService.getMMById(this.book.getMmid());
			
			StringBuffer buffer = new StringBuffer();
			buffer.append("您收到一条新的订座请求").append("\n");
			buffer.append("位置：" + this.book.getLoca().getPoi()).append("\n");
			buffer.append("就餐人数：" + this.book.getCustNum()).append("\n");
			if (book.getArriTime() != null && !book.getArriTime().equals("")) {
				buffer.append("到店时间：" + book.getArriTime()).append("\n");
			}
			if (book.getCustName() != null && !book.getCustName().equals("")) {
				buffer.append("客人称呼：" + this.book.getCustName()).append("\n");
			}
			buffer.append("联系方式：" + this.book.getCustPhone()).append("\n\n");
			
			String redirectUrl = ConfigInfo.iamSnowmmUrl + "?mmid="+mm.getMmid();
		    buffer.append("<a href=\""+redirectUrl+"\">进行操作>></a>").append("\n");
			
			WeixinUtil.sendTextMsgCustom(mm.getOpenId(), buffer.toString());
			
		} catch (Exception e) {
			logger.error("Exception", e);
		}
		
		
		return SUCCESS;
	}
	
	
	@Action(value="booking_success",results = { @Result(type = "json") })
	public String bookingSuccess() {
		
		try {
			this.prebookService.updatePrebook(this.book.getId(), 1, this.book.getMessage());
			this.result = "success";
		} catch (Exception e) {
			this.result = "error";
			logger.error("SQLException", e);
			return SUCCESS;
		}
		
		try {
			SnowMM mm = this.mmService.getMMById(this.book.getMmid());
			StringBuffer buffer = new StringBuffer();
			buffer.append("订座成功，"+mm.getNickname()+"已帮您在“"+book.getLoca().getPoi()+"”订好了座位。").append("\n\n");
			if (book.getMessage() != null && !book.getMessage().equals("")) {
				buffer.append("MM留言：" + book.getMessage()).append("\n");
			}
			WeixinUtil.sendTextMsgCustom(book.getOpenId(), buffer.toString());
			
		} catch (Exception e) {
			logger.error("Exception", e);
		}
		
		return SUCCESS;
	}
	
	@Action(value="booking_cancel",results = { @Result(type = "json") })
	public String bookingCancel() {
		
		try {
			this.prebookService.updatePrebook(this.book.getId(), 3, this.book.getMessage());
			this.result = "success";
		} catch (Exception e) {
			this.result = "error";
			logger.error("SQLException", e);
			return SUCCESS;
		}
		
		try {
			SnowMM mm = this.mmService.getMMById(this.book.getMmid());
			StringBuffer buffer = new StringBuffer();
			buffer.append("十分抱歉，"+mm.getNickname()+"未能完成帮您在“"+book.getLoca().getPoi()+"”的订座。").append("\n\n");
			if (book.getMessage() != null && !book.getMessage().equals("")) {
				buffer.append("MM留言：" + book.getMessage()).append("\n");
			}
			WeixinUtil.sendTextMsgCustom(book.getOpenId(), buffer.toString());
			
		} catch (Exception e) {
			logger.error("Exception", e);
		}
		
		return SUCCESS;
	}
	

}
